<?php

namespace App\Http\Controllers\Mobile;

use App\Logics\AccountLogic;
use App\Service\CNNickname;

class AccountController extends BaseController
{
    // 个人账户

    // 初始化
    public function initialize()
    {
        parent::initialize();
    }

    // 退出
    public function logout()
    {
        return $this->success();
    }


    // 一般登录
    public function userLogin()
    {
        $logic = new AccountLogic();
        $rst   = $logic->userLogin();
        if($rst === false){
            return $this->error($logic->getError());
        }

        return $this->success($rst);
    }


    // 登录方式：手机号登录（只登录 没有也不注册）
    public function mobile()
    {
        $logic = new AccountLogic();
        $rst   = $logic->mobileLogin();
        if($rst === false){
            return $this->error($logic->getError());
        }

        return $this->success($rst);
    }

    // 注册
    public function reg()
    {
        $logic = new AccountLogic();
        $rst   = $logic->newReg();
        if($rst === false){
            return $this->error($logic->getError());
        }

        return $this->success($rst, '注册成功！');
    }

    // 忘记密码
    public function forgot()
    {
        $logic = new AccountLogic();
        $rst   = $logic->forgot();
        if($rst === false){
            return $this->error($logic->getError());
        }

        return $this->success($rst);
    }

    // 发送邮件验证码
    public function sendMail()
    {
        $logic = new AccountLogic();
        $rst   = $logic->sendMail();
        if($rst === false){
            return $this->error($logic->getError());
        }

        return $this->success($rst, 'email_send_success');
    }

    ########################################################################################
    // 小程序登录（如果没有注册 则自动注册）
    // 小程序获取openID（如果已经存在，则自动登录，如果不存在，在登录的时候在输入手机号重新注册）
    public function mini()
    {
        $logic = new AccountLogic();
        $rst   = $logic->miniLogin();
        if($rst === false){
            return $this->error($logic->getError());
        }

        return $this->success($rst);
    }


    // 微信公众号登录
    public function openid()
    {
        $logic = new AccountLogic();
        $rst   = $logic->openidLogin();
        if($rst === false){
            return $this->error($logic->getError());
        }

        return $this->success($rst);
    }


    // app或公众号获取openID（如果已经存在，则自动登录，如果不存在，在登录的时候在输入手机号重新注册）
    public function wx()
    {
        $logic = new AccountLogic();
        $rst   = $logic->wxLogin();
        if($rst === false){
            return $this->error($logic->getError());
        }

        return $this->success($rst);
    }

    // 生成token
    public function token()
    {
        $logic = new AccountLogic();
        $rst   = $logic->token();
        if($rst === false){
            return $this->error($logic->getError());
        }

        return $this->success($rst);
    }

    // 随机生成昵称
    public function nick()
    {
        //$faker = \Faker\Factory::create('zh_CN');
        //echo $randomNickname = $faker->userName();
        $nickname = (new CNNickname())->make();
        $key      = '';

        return $this->success($nickname, 'random_nickname_has_been_successfully');
    }


}


